package com.tao.mytestsocket

import com.tao.mytestdd.datagrama.DataEntity
import com.tao.mytestdd.datagrama.DatagramService
import com.tao.mytestdd.datagrama.UDPSendManager

/**
 *reate by tao on{DATE}
 */
class UDPTest : HttpTest {
    override val tag = "UDP服务"
    var UDP: DatagramService? = null
    val udpSender: UDPSendManager = UDPSendManager()

    constructor(act: ILog) : super(act) {
        Thread {
            udpSender.local_service_host = NetUtils.getLocalIPAddress().hostName
        }.start()
        udpSender.local_service_port = localPort
    }

    override fun open() {
        close()
        UDP = DatagramService()
        UDP?.call = MyUdpCall()
        UDP?.init(localPort)
    }

    inner class MyUdpCall : DatagramService.DatagramCall {
        override fun onOpen() {
            udpSender.local_service_port = localPort

            act.appendText("$tag 端口:$localPort  服务开启 ")
        }

        override fun onClose() {
            act.appendText("$tag 端口:$localPort  服务关闭 ")

        }

        override fun onError(msg: String, exception: Exception?) {
            act.appendText("$tag 端口:$localPort  服务异常 ${msg}  ${exception?.toString()}")

        }

        override fun onReceiver(msg: DataEntity<String?>?) {
            msg?.let {
                act.appendText("$tag 收到数据   ${it.obj?.substring(17,30)} 数据长度： ${it.obj?.length} \n 数据内容：{ ${if (it.obj?.length!! >100) {"${it.obj?.subSequence(0,100)} ...."} else it.obj}}  ")
                try {
                    removeTimerData(it.obj?.substring(17,30)!!)
                } catch (e: Exception) {
                }
                if (msg.isFromServer)
                    return
                try {
                    val ms ="服务端：" + NetUtils.getLocalIPAddress().hostName + " ${it.obj?.substring(17,30)}  收到数据内容{ ${it.obj}}";
                    act.appendText("$tag  回复数据 目标：${it.host}:${it.port}  ${it.obj?.substring(17,30)} 数据长度：${it.obj?.length}  消息内容 { ${if (ms.length >100) {"${ms.subSequence(0,100)} ...."} else ms} }")
                    udpSender.sendMag( ms,it.host,it.port ,true )
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }
        }

        override fun onSend(msg: String) {
//            act.appendText("$tag  数据发送完成 :  ${msg} ")
            
        }

    }

    override fun close() {
        UDP?.close()
        UDP = null
    }

    override fun send(msg: String) {
        if (loopIng) return
        executors.execute {
            try {
                do {
                    if (send_loop) {
                        loopIng = true
                    }
                    try {
                        val msg1 = "客户端：" + NetUtils.getLocalIPAddress().hostName + " 消息ID：${msgID++}    \n    $msg} "
                        act.appendText("$tag  发送数据 目标：$serverIp:$serverPort msgId:${msgID-1}  数据长度：${msg1.length}  消息内容 { ${if (msg.length >100) {"${msg.subSequence(0,100)} ...."} else msg} }")
                        udpSender.sendMag(msg1, serverIp, serverPort)
                        pushTimer(TimerData(msg1 , "${msgID-1}"))
                        Thread.sleep( if (msg.length >1024)(msg.toByteArray().size /500).toLong() else 100)
                    } catch (e: Exception) {
                        e.printStackTrace()
                    }
                } while (send_loop)
            } finally {
                loopIng = false
            }
        }
    }


}